<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class memberwalletrechargeModel extends Model
{
    protected $table = 'member_wallet_recharge';
    protected $primaryKey = 'id';
    protected $appends = [];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function notifyProcess($payType, $notifyData)
    {
        Log::out("Notify paytype {$payType}", 'notify');
        $order = self::where('sn', $notifyData['out_trade_no'])->first();
        if ($order && $order['status'] == 0) {
            DB::transaction(function () use ($order, $notifyData) {
                $user = memberModel::find($order['member_id']);
                self::where('id', $order['id'])->update([
                    'before'       => $user->money,
                    'after'        => $user->money + $order['money'],
                    'status'       => 1,
                    'payment_json' => json_encode($notifyData),
                ]);
                memberModel::money($order['money'], $user->id, 'recharge', $order->id, '钱包充值');
            });
        } else {
            Log::out("ordersn {$notifyData['out_trade_no']} does not exist", 'notify');
        }
        return true;
    }
}
